package com.example.tse.rbhttpservice.service.moments;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsActivityGetList;
import com.example.tse.rbhttpservice.service.moments.model.MomentsActivityGetListRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsActivityGetListResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsActivityGetMomentList;
import com.example.tse.rbhttpservice.service.moments.model.MomentsActivityGetMomentListRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsActivityGetMomentListResponse;
import com.example.tse.rbhttpservice.service.moments.model.MomentsActivityGetRequest;
import com.example.tse.rbhttpservice.service.moments.model.MomentsActivityGetResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 
 * @author wliu
 *    edi  tse
 */
public class MomentsActivityServiceImpl implements MomentsActivityService {
	private Context context;
	private static Gson gson = new Gson();
	
	
	public MomentsActivityServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 获取朋友圈活动详情
	 * @param request
	 * @param listener
	 * @return MomentsActivityGetResponse
	 */		
	 
	    public void get(MomentsActivityGetRequest request, final RemoteServiceListenerT<MomentsActivityGetResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("activityId",request.getActivityId());

        final String url = "moments/activity/get";


            new HttpService().get(context, url, params, MomentsActivityGetResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MomentsActivityGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MomentsActivityGetResponse() : (MomentsActivityGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MomentsActivityGetResponse() : (MomentsActivityGetResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
	/**
	 * 可按最热、最新关键字排序（返回值参照接口详情）
	 * @param request
	 * @param listener
	 * @return MomentsActivityGetMomentListResponse
	 */		
	 
	    public void getMomentList(MomentsActivityGetMomentListRequest request, final RemoteServiceListenerT<MomentsActivityGetMomentListResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("activityId",request.getActivityId());
            params.put("sort",request.getSort());

        final String url = "moments/activity/getMomentList";


        
        /**
        activity
        getMomentList
        GetMomentList
        可按最热、最新关键字排序（返回值参照接口详情）
        GET
        MomentsActivity
        MomentsActivityGetMomentList
        moments/activity/getMomentList
        array
        */
	
           final Type type = new TypeToken<List<MomentsActivityGetMomentList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MomentsActivityGetMomentListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MomentsActivityGetMomentList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MomentsActivityGetMomentListResponse responce = new MomentsActivityGetMomentListResponse();
                 responce.setGetMomentList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MomentsActivityGetMomentListResponse());
                 } else {
                     List<MomentsActivityGetMomentList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsActivityGetMomentListResponse responce = new MomentsActivityGetMomentListResponse();
                     responce.setGetMomentList(items);
                     listener.okData(rudderResponse == null ? new MomentsActivityGetMomentListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MomentsActivityGetMomentListResponse(), fromWhere);
                 } else {
                     List<MomentsActivityGetMomentList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsActivityGetMomentListResponse responce = new MomentsActivityGetMomentListResponse();
                     responce.setGetMomentList(items);
                     listener.okData(rudderResponse == null ? new MomentsActivityGetMomentListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
	/**
	 * 在首页点击活动进入，显示活动列表，包含字段活动标题（话题＋标题），活动介绍等
	 * @param request
	 * @param listener
	 * @return MomentsActivityGetListResponse
	 */		
	 
	    public void getList(MomentsActivityGetListRequest request, final RemoteServiceListenerT<MomentsActivityGetListResponse> listener) {

        final HttpParams params = new HttpParams();
        


        final String url = "moments/activity/getList";


        
        /**
        activity
        getList
        GetList
        在首页点击活动进入，显示活动列表，包含字段活动标题（话题＋标题），活动介绍等
        GET
        MomentsActivity
        MomentsActivityGetList
        moments/activity/getList
        array
        */
	
           final Type type = new TypeToken<List<MomentsActivityGetList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MomentsActivityGetListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MomentsActivityGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MomentsActivityGetListResponse responce = new MomentsActivityGetListResponse();
                 responce.setGetList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MomentsActivityGetListResponse());
                 } else {
                     List<MomentsActivityGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsActivityGetListResponse responce = new MomentsActivityGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MomentsActivityGetListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MomentsActivityGetListResponse(), fromWhere);
                 } else {
                     List<MomentsActivityGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MomentsActivityGetListResponse responce = new MomentsActivityGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MomentsActivityGetListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
}